﻿using SistemaAdministracion.Models.BaseClass;
using SistemaAdministracion.Models.PatternBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace SistemaAdministracion.Models.Authentication
{
    public class CustomRoleProvider : RoleProvider
    {
        IDbContext _context;
        UnitOfWork _UoW;
        IRepository<Role> _repRole;
        IRepository<Usuario> _repUser;
        IRepository<UsuarioRol> _repUserRole;

        public CustomRoleProvider()
        {
            this._context = new DbContextAdapter(ContextBase.ContextBaseInstance);
            this._UoW = new UnitOfWork(_context);
            this._repRole = new BaseRepository<Role>(_context);
            this._repUser = new BaseRepository<Usuario>(_context);
            this._repUserRole = new BaseRepository<UsuarioRol>(_context);
        }


        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            IEnumerable<Role> roles = _repRole.GetAll();
            string[] AllRoles = roles.Select(r => r.NombreRol).ToArray();
            return AllRoles;
        }
        //Busqueda de roles por usuario
        public override string[] GetRolesForUser(string username)
        {
            Usuario u = _repUser.SingleOrDefault(us => us.UserName == username);
            UsuarioRol usr = _repUserRole.SingleOrDefault(us => us.idUser == u.IdUser);
            Role rl = _repRole.SingleOrDefault(r => r.IdRole == usr.idRole);
            if (u == null)
                return new string[] { };
            return u.UsuarioRol == null ? new string[] { } : u.UsuarioRol.Select(o => o.Role).Select(o => o.NombreRol).ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var us = _repUser.SingleOrDefault(u => u.UserName == username);
            if (us != null)
            {
                return us.UsuarioRol != null && us.UsuarioRol.Select(u => u.Role).Any(r => r.NombreRol == roleName);
            }
            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}